package de.bbl.android.adapter.DataCenter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.bbl.android.R;
import de.bbl.android.classes.TableTeam;
import de.bbl.android.tools.Tools;

public class DataCenterTableArrayAdapter extends ArrayAdapter<TableTeam> {

	private final Activity context;
	private final TableTeam[] values;

	static class ViewHolder {
		public TextView Id;
		public ImageView TeamImage;
		public TextView Team;
		public TextView Played;
		public TextView Won;
		public TextView Streak;
	}
	
	public DataCenterTableArrayAdapter (Activity context, TableTeam[] values) {
		super(context, R.layout.row_layout_datacenter_table, android.R.id.text1, values);
		this.context = context;
		this.values = values;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.row_layout_datacenter_table, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.Id = (TextView) rowView.findViewById(R.id.textView_datacenter_table_pos);
			viewHolder.TeamImage = (ImageView) rowView.findViewById(R.id.imageView_datacenter_table_image);
			viewHolder.Team = (TextView) rowView.findViewById(R.id.textView_datacenter_table_team);
			viewHolder.Played = (TextView) rowView.findViewById(R.id.textView_datacenter_table_played);
			viewHolder.Won = (TextView) rowView.findViewById(R.id.textView_datacenter_table_won);
			viewHolder.Streak = (TextView) rowView.findViewById(R.id.textView_datacenter_table_streak);
			rowView.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();
		
		TableTeam currentTeam = values[position];
		
		holder.Id.setText(currentTeam.getIndex());
		holder.Team.setText(currentTeam.getTeamName());
		holder.TeamImage.setImageResource(Tools.loadTeamImage(currentTeam.getTeamID()));
		holder.Played.setText(currentTeam.getPlayed());
		holder.Won.setText(currentTeam.getWon());
		holder.Streak.setText(currentTeam.getWinningStreak());
		return rowView;
	}
}
